package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.mapper.CategoryMapper;
import com.li.youhappygo.model.dto.CategoryQueryParamDto;
import com.li.youhappygo.model.entity.commond.Category;
import com.li.youhappygo.model.entity.user.Users;
import com.li.youhappygo.model.vo.category.CategoryVo;
import com.li.youhappygo.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageInfo<CategoryVo> getCategoryList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> categoryList =categoryMapper.selectList(null);
//        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for(Category category : categoryList){
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            Category category1 = categoryMapper.selectOne(new QueryWrapper<Category>().eq("id", category.getParentId()));
            categoryVo.setParentName(category1.getName());
            categoryVoList.add(categoryVo);
        }
        PageInfo<CategoryVo> pageInfo = new PageInfo<>(categoryVoList);
        return pageInfo;

    }

    @Override
    public void insertCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void deleteCategory(Long id) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        categoryMapper.delete(queryWrapper);
        categoryMapper.deleteById(id);

    }

    @Override
    public void updateCategory(Category category) {
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",category.getId());
        categoryMapper.update(category,updateWrapper);

    }

    @Override
    public List<Category> getChildCategory(Long categoryId) {
        QueryWrapper<Category> queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("parent_id",categoryId);
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public PageInfo<CategoryVo> getCategoryListByQueryParam(CategoryQueryParamDto categoryQueryParamDto,
                                                            Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        PageHelper.startPage(pageNum,pageSize);
        if(categoryQueryParamDto.getName()!=null){
            categoryQueryParamDto.setName("%" + categoryQueryParamDto.getName() + "%");
        }
//        categoryQueryParamDto.setName("%" + categoryQueryParamDto.getName() + "%");
        if(categoryQueryParamDto.getParentName()!=null){
            categoryQueryParamDto.setParentName("%" + categoryQueryParamDto.getParentName() + "%");
        }

        List<Category> categoryList = categoryMapper.selectByQueryParam(categoryQueryParamDto);
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for(Category category : categoryList){
            CategoryVo categoryVo = new CategoryVo();
//            BeanUtils.copyProperties(category,categoryVo);
            if(category!=null){
                categoryVo.setId(category.getId());
                categoryVo.setName(category.getName());
                categoryVo.setImgUrl(category.getImgUrl());
                categoryVo.setCreateTime(category.getCreateTime());
                Category category1 = categoryMapper.selectOne(new QueryWrapper<Category>().eq("id", category.getParentId()));
                categoryVo.setParentName(category1.getName());
                categoryVoList.add(categoryVo);
            }

        }
        PageInfo<CategoryVo> pageInfo = new PageInfo<>(categoryVoList);
        return pageInfo;
    }
}
